package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.anno.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.dto.UserInfoDto;
import com.atguigu.tingshu.user.client.impl.UserInfoDegradeFeignClient;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "微信授权登录接口")
@RestController
@RequestMapping("/api/user/wxLogin")
@Slf4j
public class WxLoginApiController {

    @Autowired
    private UserInfoService userInfoService;


    @Operation(summary = "小程序授权登录")
    @GetMapping("/wxLogin/{code}")
    public Result<Map<String , String>> wxLogin(@PathVariable String code) throws WxErrorException {
        Map<String , String> token = userInfoService.wxLogin(code);
        return Result.build(token , ResultCodeEnum.SUCCESS) ;
    }


    @GuiGuLogin
    @Operation(summary = "获取登录信息")
    @GetMapping("/getUserInfo")
    public Result<UserInfoVo> getUserInfo(){
        Long userId = AuthContextHolder.getUserId();			//  获取到用户Id
        UserInfoVo userInfoVo = userInfoService.getUserInfoVoByUserId(userId);		//  调用服务层方法
        return Result.ok(userInfoVo);			//  返回数据
    }

    @GuiGuLogin
    @PostMapping("/updateUser")
    public Result updateUser (@RequestBody UserInfoDto userInfoDto){

        userInfoService.updateUser(userInfoDto);
        return  Result.ok();
    }
}
